package Presentation.WebUI;

import java.util.Collection;
import java.util.Map;
import java.util.LinkedList;

public class LinksGenerator {

    public static String searchLink( String keyword, String name, String method ) {
        String res = "<a href=\"index.jsp?action=SearchMovie&amp;keyword="+keyword+"&amp;method="+method+"\">";
        res += name;
        res += "</a>";
        return res;
    }

    public static String searchLink( String keyword, String method ) {
        return searchLink(keyword,keyword,method);
    }

    public static String userLink( String username ) {
        String res = "<a href=\"index.jsp?action=ShowUser&amp;userName="+username+"\">";
        res += username;
        res += "</a>";
        return res;
    }

    public static String movieLink( String movieName ) {
        String res = "<a href=\"index.jsp?action=ShowMovie&amp;movieName="+movieName+"\">";
        res += "<span class=\"movie\">"+movieName+"</span>";
        res += "</a>";
        return res;
    }
	
	public static String recommendationLink( String movieName ) {
        String res = "<a href=\"index.jsp?action=RecommendMovie&amp;movieName="+movieName+"\">";
        res += "<span class=\"recommend\">Recommend</span>";
        res += "</a>";
        return res;
    }

    public static String createSelect(Collection items, String id, String name, String selectedValue){
        String ans = "<select name=\"" + name + "\" class=\"" + id + "\" >\n";
        String selected;
        for(Object o: items){
            if(o.equals(selectedValue)) selected = "selected=\"selected\"";
            else selected = "";
            ans += "\t<option value=\""+o+"\" " + selected + " >"+o+"</option>\n";
        }

        ans += "</select>\n";

        return ans;

    }


    public static String navLink(Map<Object,Object> parameterMap, int start, String span) {
        Collection<String> except = new LinkedList<String>();
        except.add("start");
        String linkQueryString = generateQueryString(parameterMap,except);
        if(start < 0)
            return "&nbsp;";
        String ans = "<a href=\"";
        ans += "index.jsp?"+linkQueryString+"start=" + start;
        ans += "\">"+span+"</a>";
        return ans;
    }

    public static String navLinks(Map<Object,Object> parameterMap, int start, int amount){
        String prevLink = navLink(parameterMap, start - amount, "prev");
        String nextLink = navLink(parameterMap, start + amount, "next");

        return "<table><tr><td id=\"prev\">" + prevLink + "</td><td id=\"next\">" + nextLink + "</td></tr></table>";
    }

    public static String navLink(String action, String keyword, String method, int start, String span){
        if(start < 0)
            return "&nbsp;";

        String ans = "<a href=\"";
        ans += "index.jsp?action="+action+"&amp;";
        if(keyword != null && !keyword.equals("")){
            ans += "keyword=" + keyword + "&amp;";
        }
        if(method != null && !method.equals("")){
            ans += "method=" + method + "&amp;start=" + start;
        }

        ans += "\">"+span+"</a>";

        return ans;
    }

    public static String navLinks(String action, String keyword, String method, int start, int amount){
        String prevLink = navLink(action, keyword, method, start - amount, "prev");
        String nextLink = navLink(action, keyword, method, start + amount, "next");

        return "<table><tr><td id=\"prev\">" + prevLink + "</td><td id=\"next\">" + nextLink + "</td></tr></table>";
    }

	
	public static String recommendLetterNav(String action) {
        String link = "index.jsp?action="+action+"&amp;method=START_WITH&amp;keyword=";
        String ans = "<div id=\"letternav\">";
        ans += "<ul>";
        for(char c = 'a';c<='z';c++){
            ans += "<li><a href=\""+link + c + "\" title=\""+c+"\">" + c + "</a></li>";
        }
        ans += "<ul>";
        ans += "</div>";

        return ans;
    }
	
    public static String letterNav(String action,String method,String keyword) {
        String link = "index.jsp?action="+action+"&amp;method="+method+"&amp;keyword="+keyword+"&amp;startWith=";
        String ans = "<div id=\"letternav\">";
        ans += "<ul>";
        for(char c = 'a';c<='z';c++){
            ans += "<li><a href=\""+link + c + "\" title=\""+c+"\">" + c + "</a></li>";
        }
        link = "index.jsp?action="+action+"&amp;method="+method+"&amp;keyword="+keyword;
        ans += "<li><a href=\"" + link + "\" title=\"ALL\">ALL</a></li>";
        ans += "<ul>";
        ans += "</div>";

        return ans;
    }

    public static String letterNav(Map<Object,Object> parameterMap) {
        Collection<String> except = new LinkedList<String>();
        except.add("startWith");
        except.add("start");
        String linkQueryString = generateQueryString(parameterMap,except);
        String link = "index.jsp?"+linkQueryString+"startWith=";
        String ans = "<div id=\"letternav\">";
        ans += "<ul>";
        for(char c = 'a';c<='z';c++){
            ans += "<li><a href=\""+link + c + "\" title=\""+c+"\">" + c + "</a></li>";
        }
        link = "index.jsp?"+linkQueryString;
        ans += "<li><a href=\"" + link + "\" title=\"ALL\">ALL</a></li>";
        ans += "<ul>";
        ans += "</div>";

        return ans;        
    }

    private static String generateQueryString(Map<Object,Object> parameterMap, Collection<String> except) {
        String linkQueryString = "";
        for (Object key: parameterMap.keySet()) {
            if (!except.contains(key)) {
                String[] values = (String[]) parameterMap.get(key);
                linkQueryString += key.toString()+"="+values[0]+"&amp;";
            }
        }
        return linkQueryString;
    }

    public static String showGraphLink(String m) {
        String res = "<a href=\"index.jsp?action=MovieRankDistribution&amp;movieName="+m+"\">";
        res += "<span class=\"showGraph\">Show Graph</span>";
        res += "</a>";
        return res;        
    }

    public static String showUserRated(String m, String s) {
        String res = "<form action=\"index.jsp\" method=\"post\">";
        res += s;
        res += "<input type=\"hidden\" name=\"action\" value=\"UsersRatedMovie\" />";
        res += "<input type=\"hidden\" name=\"movieName\" value=\""+m+"\" />";
        res += "<input type=\"submit\" value=\"Users gave rate \" class=\"reportButton\" />";
        res += "<select name=\"rank\" id=\"rank\" >";
        for (int i=1; i<=10; i++){
            res += "<option value=\""+i+"\">"+i+"</option>";
        }
        res += "</select>";
        res += "(at least)";
        res += "</form>";
        return res;
    }

    public static String showListWithNavButtons(String[] show, Map<Object,Object> parameterMap, int start, int amount, String last) {
        String res = "";
        if(show.length != 0){
            res += "<ul>";
            for (String s : show) {
                res += "<li>" + s + "</li>";
            }
            res += "</ul>";

            res += navLinks(parameterMap, start, amount);
        }
        else{
            res += last;
            res += navLink(parameterMap, start - amount, "prev");
        }        
        return res;
    }

    public static String showTable(String[] show, Map<Object,Object> parameterMap, int start, int amount, String last) {
        String res = "";
        if(show.length != 0){
            res += "<table>";
            for (String s : show) {
                res += s;
            }
            res += "</table>";
        }
        else{
            res += last;
        }
        return res;
    }
}
